/**
 * Created by Tuffy on 16/1/29.
 */
'use strict';

angular.module('starter.controllers')
    .controller('PurchaseCtrl', ['$scope','$rootScope', '$timeout',  'ReportPurchaseService',  'ionicMaterialMotion', 'ionicMaterialInk','$state',
        function($scope,$rootScope, $timeout, ReportPurchaseService,  ionicMaterialMotion, ionicMaterialInk,$state) {

            $rootScope.purchase = {};

            $scope.purchase = {
                search: {
                    value:'',
                    filter:{},
                    doAct:function(){
                        if(this.timer){
                            $timeout.cancel(this.timer);
                        }
                        this.timer = $timeout(function(){
                            $scope.purchase.refresh();
                        },500);
                    }
                },
                page: {
                    pageNum:1,
                    pageSize:10,
                    total:200
                },
                loaded:true,
                data:[
                    /*{
                     hotelName:"测试酒店",
                     channel:"去哪",
                     orderId:"1234567",
                     amout:"1000.00",
                     orderStatus:"已下单",
                     payType:"到付",
                     sukNum:3,
                     sukName:"商品",
                     orderTime:"2016-02-19 23:09"
                     }*/
                ],
                loadData:function(okFunc){
                    var _this = this,
                        params = {
                            hotelName:_this.search.value,
                            pageNum:_this.page.pageNum,
                            pageSize:_this.page.pageSize
                        };
                    ReportPurchaseService.findPurchaseList(params)
                        .success(function(res){
                            if(res.success){
                                if(typeof (okFunc) == 'function'){
                                    okFunc();
                                }
                                _this.data = _this.data.concat(res.attribute.data.list);
                                _this.page.total = res.attribute.data.count;
                            }else{
                                _this.page.total = 0;
                                console.log('加载失败');
                            }
                        }).error(function(error){
                        _this.page.total = 0;
                        console.log(error);
                    }).finally(function(){
                        _this.loaded = true;
                        $scope.$broadcast('scroll.refreshComplete');
                        $scope.$broadcast('scroll.infiniteScrollComplete');
                        $timeout(function() {
                            ionicMaterialMotion.fadeSlideIn();
                        }, 200);

                        $timeout(function() {
                            ionicMaterialInk.displayEffect();
                        }, 1000);
                    });
                },
                moreDataCanBeLoaded:function(){
                    return this.page.total > this.page.pageNum*this.page.pageSize;
                },
                loadMore:function(){
                    this.page.pageNum++;
                    this.loadData();
                },
                refresh:function(){
                    var _this = this;
                    _this.page.pageNum = 1;
                    _this.loadData(function(){
                        _this.data = [];
                    });
                }
            } ;


            $scope.purchase.loadData();

            $scope.goDetailMain = function(purch){
                // 详细信息
                $rootScope.purchase.detailData = purch;
                $state.go('app.chars-purchase-detail', {orderId: purch.id});
            }

        }])
    .controller('PurchaseDetailCtrl', ['$scope','$rootScope', '$timeout',  'ReportPurchaseService', 'ionicMaterialMotion', 'ionicMaterialInk','$state',
        function($scope,$rootScope, $timeout, ReportPurchaseService, ionicMaterialMotion, ionicMaterialInk,$state) {
            $scope.purchaseDetail = $rootScope.purchase.detailData;
            /*$scope.purchaseDetail = {
             hotelName:"测试酒店",
             channel:"去哪",
             orderName:"李雪楠",
             orderNamePhone:"13301360605",
             orderId:"1234567",
             amout:"1000.00",
             orderStatus:"已下单",
             payType:"到付",
             skuNum:3,
             sukName:"商品",
             orderTime:"2016-02-19 23:09"
             }*/

        }])
    .service('ReportPurchaseService',['CrmHttp','$rootScope',function(CrmHttp,$rootScope){
        var url = '/crm/crmHotel/';
        return {
            /**
             * 分销酒店全部数据
             * @param params
             * @returns {*}
             */
            findPurchaseList: function (params) {
                var act = 'findOmsOrderList';
                    params['marketid'] = $rootScope.localStorageObj.userId;
                return CrmHttp.post(url + act, params);
            },
            /**
             * 分销酒店明细数据
             * @param params
             * @returns {*}
             */
            findShoppingDetailByOrderId: function (params) {
                var act = 'findShoppingDetailByOrderId';
                return CrmHttp.post(url + act, params);
            }
        };
    }]);